var app = getApp();
Page({
  data: {
    userName: '',
    userPassword: '',
    userID: '',
    phone: '',
    realName: '',
    card: '',
    timecurrency: 0,
    servicetimes: 0,
    gotlike: 0,
    audittask: 0,
    unfinishedtask: 0,
    finishedtask: 0,
    auditexchange: 0,
    unfinishedexchange: 0,
    finishedexchange: 0,
    postedcomment: 0,
    gotfeedback: 0,
    exchangecomment: 0,
    checkbox: false,
    serveTime:0,
    qiandao:null,
    qiantui:null,
    numList: [{
      name: '创建用户'
    },{
      name: '完善个人信息'
    }],
    num: 1,
    scroll: 0,
    columns: ["未知","男", "女"],
    gender: 0 || wx.getStorageSync("gender") * 1,
  },
  // 返回主界面
  backHomeTap: function() {
    wx.switchTab({
    url: '../home/home',
    })
  },
  // 手机号
  phoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  // 真实姓名
  nameInput: function(e) {
    this.setData({
      realName: e.detail.value
    });
  },
  // 身份证
  cardInput: function(e) {
    this.setData({
      card: e.detail.value
    })
  },
  //性别
  pickSex: function(e) {
    this.setData({
      gender: e.detail.value
    });
  },
  // 下一步完成
  registerSuccessTap: function() {
    var genderarr = ["未知","男", "女"];
    var gender = this.data.gender;
    var phone = this.data.phone;
    var realName = this.data.realName;
    var card = this.data.card;
    var userName = app.userName;
    var userID = app.userID;
    var userPassword = app.userPassword;
    var timecurrency = this.data.timecurrency;
    var servicetimes = this.data.servicetimes;
    var gotlike = this.data.gotlike;
    var audittask = this.data.audittask;
    var unfinishedtask = this.data.unfinishedtask;
    var finishedtask = this.data.finishedtask;
    var auditexchange = this.data.auditexchange;
    var unfinishedexchange = this.data.unfinishedexchange;
    var finishedexchange = this.data.finishedexchange;
    var postedcomment = this.data.postedcomment;
    var gotfeedback = this.data.gotfeedback;
    var exchangecomment = this.data.exchangecomment;
    var serveTime = this.data.serveTime;
    var qiandao = this.data.qiandao;
    var qiantui = this.data.qiantui;
    var phonereg = /^1[345789]\d{9}$/;
    var namereg = /^[\u4E00-\u9FA5]+$/;
    var cardreg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
    var genderzh = genderarr[gender];
    var that = this;
    if (phone === '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000,
        mask: true
      });
    } else if (!phonereg.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (!namereg.test(realName)) {
      wx.showToast({
        title: '请输入正确的名字',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (card === '') {
      wx.showToast({
        title: '请输入身份证',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (!cardreg.test(card)) {
      wx.showToast({
        title: '请输入正确的身份证',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    }else if (gender === 0) {
      wx.showToast({
       title: '请选性别',
       icon: 'none',
       duration: 2000,
       mask: true
      })
    }else {
    //注册成功后转跳
    wx.showToast({
      title: '注册成功',
      icon: 'none',
      duration: 2000,
      mask: true
    })
    wx.switchTab({
      url: '../home/home',
    })
    // 初始化云
    wx.cloud.init({
      traceUser: true
    });
    // 初始化数据库
    const db = wx.cloud.database();
    db.collection('userInfo').add({
      // data 字段表示需新增的 JSON 数据
      data: {
      realName: realName,
      userName: userName,
      userPassword: userPassword,
      userID: userID,
      phone: phone,
      card: card,
      gender: genderzh,
      timecurrency: timecurrency,
      servicetimes: servicetimes,
      gotlike: gotlike,
      audittask: audittask,
      unfinishedtask: unfinishedtask,
      finishedtask: finishedtask,
      auditexchange: auditexchange,
      unfinishedexchange: unfinishedexchange,
      finishedexchange: finishedexchange,
      postedcomment: postedcomment,
      gotfeedback: gotfeedback,
      exchangecomment: exchangecomment,
      serveTime:serveTime,
      qiandao:qiandao,
      qiantui:qiantui
      },
      success: function(res) {
      console.log(res);
      console.log(res.errMsg);
      }
    })
    }
  },
})